package com.yang.controller;

import com.yang.pojo.Product;
import com.yang.pojo.User;
import com.yang.service.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    @Qualifier("ProductServiceImpl")
    private ProductServiceImpl productService;
    private List<Product> list =new ArrayList<Product>();//购物车提示
    @RequestMapping("/queryProductByName")
    public String queryProductByName(Product product, HttpServletRequest request) {
        Product product1= productService.queryProductByName(product.getProductName());
        HttpSession session=request.getSession();
        session.setAttribute("product",product1);
        session.removeAttribute("addCarTip");
        if(product1 == null){
            session.setAttribute("error","未查到");
            return "indexq";
        }
        return "product";
    }
    @RequestMapping("/toProduct")
    public String Product(HttpServletRequest request) {
        return "product";
    }
    @RequestMapping("/toReProduct")//重新加载
    public String toReProduct(){
        return "product";
    }

    @RequestMapping("/allToProduct")//全部页面到详情
    public String AlltoProduct(HttpServletRequest request) {
        String name=request.getParameter("name");
        Product product1= productService.queryProductByName(name);
        HttpSession session=request.getSession();
        session.setAttribute("product",product1);
        session.removeAttribute("addCarTip");
        return "product";
    }
    @RequestMapping("/toTest")
    public String Test(HttpServletRequest request,Model model) {
        return "test";
    }
    @RequestMapping("/test")
    public String test(HttpServletRequest request, @RequestParam("file") CommonsMultipartFile file) throws IOException {
        return "redirect:/product/toTest";
    }
    @RequestMapping("/toCar")
    public String toCar(){
        return "cart";
    }

    @RequestMapping("/addCar")
    public String addCar(HttpServletRequest request,Product product){
        int totalprice=0;
        HttpSession session=request.getSession();
        Product product1=productService.queryProductByName(product.getProductName());
        product1.setNum(product.getNum());
        if(session.getAttribute("carList")!=null){
           list=(List<Product>) session.getAttribute("carList");
            for (Product product2 : list) {
                totalprice+=product2.getProductCounts();
                if (product2.getProductName().equals(product1.getProductName())){
                    session.setAttribute("addCarTip","已在购物车中");
                    return "redirect:/product/toProduct";
                }
            }
            totalprice+=product1.getProductCounts();
            list.add(product1);
        }else {
            totalprice+=product1.getProductCounts();
            list.add(product1);
        }
        session.setAttribute("totalprice",totalprice);
        session.setAttribute("addCarTip","添加成功");
        session.setAttribute("carList",list);
        return "redirect:/product/toReProduct";
    }
    @RequestMapping("/toAllProduct")
    public String toAllProduct(HttpServletRequest request){
        HttpSession session=request.getSession();
        List<Product> list=new ArrayList<>();
        list=productService.allProduct();
        session.setAttribute("allProductNum",list.size());
        session.setAttribute("allProduct",list);
        session.setAttribute("kind","所有商品");
        return "allProduct";
    }
    @RequestMapping("/toAllProduct1")
    public String toAddProduct1(HttpServletRequest request){
        String name=request.getParameter("kind");
        System.out.println(name);
        HttpSession session=request.getSession();
        session.setAttribute("kind",name);
        List<Product> list=productService.queryProductByKind(name);
        session.setAttribute("allProductNum",list.size());
        session.setAttribute("allProduct",list);
        return "allProduct";
    }
    @RequestMapping("/toAddProduct")
    public String toAddProduct(){
        return "addProduct";
    }
    @RequestMapping("/addProduct")
    public String addProduct(Product product,HttpServletRequest request, @RequestParam("file") CommonsMultipartFile file) throws IOException {
        String name = new String(product.getProductName().getBytes("ISO-8859-1"),"utf-8");
        String kind = new String(product.getKind().getBytes("ISO-8859-1"),"utf-8");
        String detail = new String(product.getDetail().getBytes("ISO-8859-1"),"utf-8");
        product.setProductName(name);
        product.setKind(kind);
        product.setDetail(detail);
        productService.addProduct(product);
        //获取文件名 : file.getOriginalFilename();
        String uploadFileName = product.getProductName();
        //如果文件名为空，直接回到首页！
        //上传路径保存设置
        String path = request.getRealPath("/upload");
        //如果路径不存在，创建一个
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }
        InputStream is = file.getInputStream(); //文件输入流
        OutputStream os = new FileOutputStream(new File(realPath,uploadFileName));//文件输出流
        //读取写出
        int len=0;
        byte[] buffer = new byte[1024];
        while ((len=is.read(buffer))!=-1){
            os.write(buffer,0,len);
            os.flush();
        }
        os.close();
        is.close();
        return "addProduct";
    }

    @RequestMapping("/clearCar")//购物车清除一件商品
    public String clearCar(HttpServletRequest request) {
        String name = request.getParameter("name");
        HttpSession session=request.getSession();
        List<Product> list=(List<Product>) session.getAttribute("carList");
        int price = 0;
        for(int i = list.size() - 1; i >= 0; i--){
            Product product = list.get(i);
            if(name.equals(product.getProductName())){
                price=product.getProductCounts();
                list.remove(product);
            }
        }
        int totalprice=(int)session.getAttribute("totalprice")-price;
        session.setAttribute("totalprice",totalprice);
        session.setAttribute("carList",list);
        return "cart";
    }
    @RequestMapping("/clearAllCar")//购物车清空
    public String clearAllCar(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.removeAttribute("carList");
        session.removeAttribute("totalprice");
        return "cart";
    }
}
